package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.mapper.HelpCenterMapper;
import com.doubi.model.sys.TAdver;
import com.doubi.model.sys.THelpCenter;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import com.doubi.vo.AppUiModel;
import com.doubi.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class HelpCenterService extends ServiceImpl<HelpCenterMapper, THelpCenter> {
    @Autowired
    private HelpCenterMapper helpCenterMapper;


    /**
     * MrDoubi
     * 2020年3月10日 14:48:43
     * 获取帮助中心信息
     * @return list
     */
    public List<THelpCenter> getHelpCenterList(int type){
        return helpCenterMapper.getHelpCenterList(type);
    }


    public PageVo<THelpCenter> getPage(PageVo<THelpCenter> pageVo, THelpCenter helpCenter){
        QueryWrapper<THelpCenter> wrapper = new QueryWrapper<>();
        if(DataUtil.isNotEmpty(helpCenter.getType())){
            wrapper.eq("type",helpCenter.getType());
        }
        wrapper.orderByAsc("create_time");
        helpCenterMapper.selectPage(pageVo,wrapper);
        return pageVo;
    }


    public AppModel delBean(String oid){
        THelpCenter helpCenter = helpCenterMapper.selectById(oid);
        if(DataUtil.isNotEmpty(helpCenter)){
            helpCenterMapper.deleteById(helpCenter.getOid());
            return AppModel.success();
        }else {
            return AppModel.error();
        }
    }

    public AppModel saveOrUpdateBean(THelpCenter helpCenter){
        if(DataUtil.isEmpty(helpCenter.getOid())){
            helpCenter.setCreateTime(new Date());
            helpCenter.setIsDelete(1);
            helpCenterMapper.insert(helpCenter);
            return AppModel.success();
        }else {
            THelpCenter bean = helpCenterMapper.selectById(helpCenter.getOid());
            bean.setType(helpCenter.getType());
            bean.setTitle(helpCenter.getTitle());
            bean.setContent(helpCenter.getContent());
            bean.setUpdateTime(new Date());
            helpCenterMapper.updateById(bean);
            return AppModel.success();
        }
    }
}
